//
//  StringAndDateHandle.m
//  CheckIn
//
//  Created by ren on 13/01/2018.
//  Copyright © 2018 bryan. All rights reserved.
//

#import "StringAndDateHandle.h"
#import "CommonDefine.h"

@implementation StringAndDateHandle

+ (instancetype)sharedInstance
{
    static StringAndDateHandle *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[super allocWithZone:NULL] init];
    });
    return handle;
}

+ (id)allocWithZone:(struct _NSZone *)zone
{
    return [self sharedInstance];
}

#pragma mark - date and string transform

- (NSString *)nowToString
{
    NSDate *now = [NSDate date];
    NSString *nowString = [self dateToString:now formatter:kDateYMDHMS];
    return nowString;
}

- (NSDate *)stringToDate:(NSString *)info formatter:(NSString *)formatter
{
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    [dateFormatter setDateFormat:formatter];
    NSDate *infoDate = [dateFormatter dateFromString:info];
    return infoDate;
}

- (NSString *)dateToString:(NSDate *)date formatter:(NSString *)formatter
{
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    [dateFormatter setDateFormat:formatter];
    NSString *nowString = [dateFormatter stringFromDate:date];
    return nowString;
}

- (BOOL)sameDay:(NSString *)checkIn checkOut:(NSString *)checkOut
{
    NSDate *checkInDate = [self stringToDate:checkIn formatter:kDateYMD];
    NSDate *checkOutDate = [self stringToDate:checkOut formatter:kDateYMD];
    if ([checkInDate compare:checkOutDate] == NSOrderedSame) {
        return YES;
    }
    return NO;
}

@end
